package orderProcessingProcess.client.junit;

import static org.junit.Assert.fail;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import orderProcessingProcess.exceptions.faults.UnknownCustomerFault;
import orderProcessingProcess.model.Address;
import orderProcessingProcess.model.Customer;
import orderProcessingProcess.model.Payment_Preference;
import orderProcessingProcess.server.NotifyService;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestSmsServiceClient {

	private static final QName SERVICE_NAME = new QName(
			"http://infosys.tuwien.ac.at/ait09/ass1/dto/notification", "NotificationService");
	private static final QName PORT_NAME = new QName(
			"http://infosys.tuwien.ac.at/ait09/ass1/dto/notification", "NotificationPT");

	private static NotifyService smsService;

	// Endpoint Address
	private static String smsServiceAddress = "http://localhost:8088/smsservice";

	@BeforeClass
	public static void init() {
		Service service = Service.create(SERVICE_NAME);

		// Add a port to the Service
		service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
				smsServiceAddress);

		smsService = service.getPort(NotifyService.class);

		Client client = ClientProxy.getClient(smsService);
		Endpoint cxfEndpoint = client.getEndpoint();

		cxfEndpoint.getEndpointInfo().setAddress(smsServiceAddress);

		/*
		 * security Map<String, Object> outProps = new HashMap<String,Object>();
		 * outProps.put(WSHandlerConstants.ACTION,
		 * WSHandlerConstants.USERNAME_TOKEN);
		 * outProps.put(WSHandlerConstants.USER, "name");
		 * outProps.put(WSHandlerConstants.PASSWORD_TYPE, WSConstants.PW_TEXT);
		 * outProps.put(WSHandlerConstants.PW_CALLBACK_CLASS,
		 * ClientPasswordCallbackHandler.class.getName());
		 * 
		 * WSS4JOutInterceptor wssOut = new WSS4JOutInterceptor(outProps);
		 * 
		 * cxfEndpoint.getOutInterceptors().add(new SAAJOutInterceptor());
		 * cxfEndpoint.getOutInterceptors().add(wssOut);
		 */

		// new TestDataEntry();
	}

	@Test
	public void testSmsNotifyCustomer() {

		// DataBackend db = DataBackend.getInstance();
		Customer c = this.createCustomer();

		try {
			smsService.notifyCustomer(c, createTemplateMsg(12));
		} catch (UnknownCustomerFault e) {
			fail(e.getMessage());
		} catch (orderProcessingProcess.exceptions.faults.MessageTooLongFault e) {
			fail(e.getMessage());
		}

	}

	@Test
	public void testNotifyServiceUnknownCustomer() {
		Customer c = this.createCustomer();
		c.setId("99999");

		try {
			smsService.notifyCustomer(c, createTemplateMsg(12));
			fail();
		} catch (UnknownCustomerFault e) {
			// Do nothing. The test is ok
		} catch (orderProcessingProcess.exceptions.faults.MessageTooLongFault e) {
			// Do nothing. The test is ok
		}

	}
	
	@Test
	public void testNotifyServiceTooLongMessage(){
		Customer c = this.createCustomer();

		try {
			smsService.notifyCustomer(c, createTemplateMsg(163));
			fail();
		} catch (orderProcessingProcess.exceptions.faults.MessageTooLongFault e) {
			//Do nothing. Test OK.
		}catch (UnknownCustomerFault e) {
			fail(e.getMessage());
		} 
	}

	private Customer createCustomer() {

		Address addr1 = new Address();
		addr1.setId("001");
		addr1.setStreet("1st Avenue");
		addr1.setCity("New Your");
		addr1.setHouse(1);
		addr1.setDoor(1);
		addr1.setZip_code("10000");

		Customer customer1 = new Customer();
		customer1.setId("00001");
		customer1.setName("John Foo");
		customer1.setAddress(addr1);
		customer1.setPaymentPreference(Payment_Preference.Credit_Card);

		// Customer = new Customer(0001, , address, paymentPreference)

		return customer1;

	}

	private String createTemplateMsg(int size) {
		String msg = "";

		for (int i = 0; i < (size / 3); i++) {
			msg += "abc";
		}
		return msg;
	}
}
